﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using arcchallenge.servicecontracts;

namespace arcchallenge.filereader
{
    public class TextFileReader : IFileReader
    {
        internal class Configuration
        {
            public string defaultDirectoryname;
        }

        private readonly Configuration config;

        public TextFileReader() : this(new Configuration()) {}
        internal TextFileReader(Configuration config)
        {
            this.config = config;
        }


        public void In_Configure(string defaultDirectoryname)
        {
            if (defaultDirectoryname == "") defaultDirectoryname = ".";
            if (!defaultDirectoryname.EndsWith(@"\")) defaultDirectoryname += @"\";
            this.config.defaultDirectoryname = defaultDirectoryname;
        }


        public void In_ReadFile(string filename)
        {
            if (Path.GetDirectoryName(filename) == "")
                filename = this.config.defaultDirectoryname + filename;

            using(var sr = new StreamReader(filename, Encoding.Default))
            {
                while (!sr.EndOfStream)
                    this.Out_Line(sr.ReadLine());
                this.Out_Line(null);
            }
        }

        
        public event Action<string> Out_Line;


        public void Dispose()
        {}
    }
}
